﻿using Coolzon.UI.Core.Parts;
using CPPEI.Coolzon.UI.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Coolzon.UI.Core.Runtime.Script
{
    /// <summary>
    /// 用于供脚本访问组件的类
    /// </summary>
    public class FrameScriptObject : IDisposable
    {
        WeakReference _Frame;

        private Dictionary<string, object> _dicVariables = new Dictionary<string, object>();

        public FrameScriptObject(Frame Frame)
        {
            _Frame = new WeakReference(Frame);
        }

        /// <summary>
        /// 日期对象
        /// </summary>
        DateScriptObject _date;

        /// <summary>
        /// 获取日期对象
        /// </summary>
        public DateScriptObject date
        {
            get
            {
                if (_date == null)
                {
                    _date = new DateScriptObject();
                }
                return _date;
            }
        }

        /// <summary>
        /// 获取场景对象
        /// </summary>
        /// <returns></returns>
        public object getScene()
        {
            Frame Frame = _Frame.Target as Frame;
            if (Frame == null)
            {
                return null;
            }
            Scene scene = Frame.CurrentPart as Scene;
            if (scene != null)
            {
                return scene.GetScriptObject();
            }
            else
            {
                return null;
            }
        }

        public object getComoponent(string name)
        {
            Frame frame = _Frame.Target as Frame;
            if (frame == null)
            {
                return null;
            }
            foreach (var key in frame.ComponentTable.Keys)
            {
                if(string.Equals((string)key, name))
                {
                    return ((IScriptHost)frame.ComponentTable[key]).GetScriptObject();
                }
            }
            return null;
        }

        /// <summary>
        /// 打开场景
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="argumentString">参数字符串 a=1&b=2&c=3</param>
        public void openScene(string scene, string argumentString)
        {
            //Coolzon.Event.NCL.NCLEventBus.PublishAsync(new Coolzon.Event.NCL.NCLOpenSceneEvent(SwitchMode.Replace, "", scene, OpenSceneArgs.BuildFromString(argumentString)));
        }

        /// <summary>
        /// 弹出场景
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="argumentString">参数字符串 a=1&b=2&c=3</param>
        public void popScene(string scene, string argumentString)
        {
            //Coolzon.Event.NCL.NCLEventBus.PublishAsync(new Coolzon.Event.NCL.NCLOpenSceneEvent(SwitchMode.Pop, "", scene, OpenSceneArgs.BuildFromString(argumentString)));
        }

        public void alert(string message)
        {
            System.Windows.MessageBox.Show(message);
        }

        public void Dispose()
        {
            this._Frame = null;
        }

        public void setVariable(string key, object value)
        {
            _dicVariables[key] = value;
        }

        public object getVariable(string key)
        {
            if (_dicVariables.ContainsKey(key))
            {
                return _dicVariables[key];
            }
            return null;
        }

        public object getVersion()
        {
            System.Reflection.Assembly entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
            return entryAssembly.GetName().Version.ToString(2);
        }

        #region timer
        /// <summary>
        /// 开启定时器
        /// </summary>
        /// <param name="func"></param>
        /// <param name="interval"></param>
        /// <returns></returns>
        public object startTimer(object callback, double interval)
        {
            Frame frame = this._Frame.Target as Frame;
            if (frame == null)
                return null;

            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Tag = callback;
            timer.Interval = TimeSpan.FromSeconds(interval);
            timer.Tick += OnTimerTick;
            timer.Start();
            return timer;
        }

        void OnTimerTick(object sender, EventArgs e)
        {
            System.Windows.Threading.DispatcherTimer timer = sender as System.Windows.Threading.DispatcherTimer;
            System.Action callback = timer.Tag as Action;
            callback.Invoke();
        }

        /// <summary>
        /// 关闭定时器
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void stopTimer(object timer)
        {
            Frame frame = this._Frame.Target as Frame;
            if (frame == null || timer == null)
                return;

            (timer as System.Windows.Threading.DispatcherTimer).Stop();
        }
        #endregion
    }
}
